<?php

namespace App\Admin\Controllers;

use App\Models\Crouse;
use App\Models\Crousesheet;

use App\Models\Squad;
use Illuminate\Http\Request;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Admin;
use Illuminate\Support\Facades\DB;


class CrousesheetController extends Controller
{
    private $class;

    public function __construct(Crousesheet $crousesheet)
    {
        $this->class = $crousesheet;
    }

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index(Content $content)
    {
        $class = new Admin;
        $class->disablePjax();
        $content->header('课表设置');
        $content->description('设置');
        $content->row($this->view());
        return $content;
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {

    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {

    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {

    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {

    }

    protected function view()
    {
        //$class = new Crousesheet();

        $days = array('1','2','3','4','5','6','7');
        $rows = array();

        $room = $this->class->getClassroom();
        foreach($days as $day){
            $rows[] = $this->class->createSheet($room,$day);
        }

        $crouses = $this->class->getCrouse();
        $teachers = $this->class->getTeacher();
        $tutors = $this->class->getTutor();
        return view('admin::widgets.crousesheet',compact('rows','days','crouses','teachers','tutors'));
    }

    protected function saveCrouse(Request $request)
    {
        return $this->class->saveCrouse($request);
    }

    protected function saveSetCrouse(Request $request)
    {
        return $this->class->saveSetCrouse($request);
    }

    protected function deleteCrouse(Request $request)
    {
        $record = Crousesheet::where('position',$request['position'])->where('section',$request['section'])
        ->update(['status'=>-1]);
        if($record == true){
            return array('status'=>1,'message'=>'删除成功');
        }else{
            return array('status'=>-1,'message'=>'删除失败');
        }
    }

    protected function deleteSetCrouse(Request $request)
    {
        $record = DB::table('set_crousesheets')->where('position',$request['position'])->where('section',$request['section'])->delete();
        if($record == true){
            return array('status'=>1,'message'=>'删除成功');
        }else{
            return array('status'=>-1,'message'=>'删除失败');
        }
    }

    protected function get (int $id){
        $squads = $this->class->getSquads($id);
        header('Cache-Control:max-age=7200,must-revalidate');
        return $squads;
    }

    public function setCrouse(Content $content)
    {
        $content->header('课表设置');
        $content->description('设置');
        $content->row($this->set_view());
        return $content;
    }

    protected function set_view()
    {
        $days = array('1','2','3','4','5','6','7');
        $rows = array();

        $room = $this->class->getClassroom();
        foreach($days as $day){
            $rows[] = $this->class->createSetSheet($room,$day);
        }

        $squads = Squad::all()->toArray();
        array_unshift($squads,['id'=>'all','name'=>'全部']);

        $crouses = $this->class->getCrouse();
        $teachers = $this->class->getTeacher();
        $tutors = $this->class->getTutor();
        return view('admin.set_crousesheet',compact('rows','days','crouses','teachers','tutors','squads'));
    }

    public function getBySquad(Request $request)
    {
        //选出已经给班级配置的课程
        //$arr[$k] = $v
        //$k:课程种类     $v:课程出现的次数
        if($request['squad'] == 'all'){
            $crouses = DB::table('squad_crouses')->GroupBy(['squad_id','crouse_id'])
                ->select(DB::raw('count(*) as num'),'crouse_id','squad_id')->get();
        }elseif($request['squad'] > 0){
            $crouses = DB::table('squad_crouses')->GroupBy(['squad_id','crouse_id'])->where('squad_id',$request['squad'])
                ->select(DB::raw('count(*) as num'),'crouse_id','squad_id')->get();
        }

        $crouse_arr = array();
        foreach($crouses as $v){
            if(!isset($crouse_arr[$v->squad_id])){
                $crouse_arr[$v->squad_id] = array();
            }
            $crouse_arr[$v->squad_id][$v->crouse_id] = $v->num;
        }

        //选出课表内已生成的课程
        //$arr[$k] = $v
        //$k:课程种类     $v:课程出现的次数
        //这里的数据只会小于等于上面的数据
        if($request['squad'] == 'all'){
            $set_crouses = DB::table('set_crousesheets')->GroupBy(['squads_id','crouse_id'])
                ->select(DB::raw('count(*) as num'),'crouse_id','squads_id as squad_id')->get();
        }elseif($request['squad'] > 0){
            $set_crouses = DB::table('set_crousesheets')->GroupBy(['squads_id','crouse_id'])->where('squads_id',$request['squad'])
                ->select(DB::raw('count(*) as num'),'crouse_id','squads_id as squad_id')->get();
        }
        $set_crouse_arr = array();
        foreach($set_crouses as $v){
            if(!isset($set_crouse_arr[$v->squad_id])){
                $set_crouse_arr[$v->squad_id] = array();
            }
            $set_crouse_arr[$v->squad_id][$v->crouse_id] = $v->num;
        }


        //找出还没有配置的课程
        $return = array();
        foreach($crouse_arr as $k=>$v){
            foreach($v as $_k => $_v){
                if(isset($set_crouse_arr[$k][$_k])){
                    $a = $_v-$set_crouse_arr[$k][$_k];
                    if($a == 0){
                        unset($crouse_arr[$k][$_k]);
                    }else{
                        $crouse_arr[$k][$_k] = $a;
                     }
                }
            }
        }

        if(count($crouse_arr) == 0){
            return array('status'=>-1,'message'=>'没有需要配置的课程');
        }

        $tutor = DB::table('admin_users as au')->join('squads as s','s.class_teacher','=','au.id')
            ->whereIn('s.id',array_keys($crouse_arr))->select('au.en_name','au.id','s.id as squad','s.name')->get();

        $tutor_arr = array();
        foreach($tutor as $v){
            $tutor_arr[$v->squad]['name'] = $v->en_name;
            $tutor_arr[$v->squad]['tutor'] = $v->id;
            $tutor_arr[$v->squad]['squad'] = $v->name;
        }

        $name = Crouse::all()->pluck('name','id');

        foreach($crouse_arr as $k=>$v){
            foreach($v as $_k => $_v){
                for($i=0;$i<$_v;$i++){
                    $return[] = array('squad_id'=>$k,'name'=>$name[$_k],'crouse_id'=>$_k,'tutor_id'=>$tutor_arr[$k]['tutor'],'tutor_name'=>$tutor_arr[$k]['name'],'squad_name'=>$tutor_arr[$k]['squad']);
                }
            }
        }

        return array('data'=>$return,'tutor'=>$tutor);
    }

    protected function UploadCrouse()
    {
        $set = DB::table('set_crousesheets')->get();

        $insert = array();

        foreach($set as $v){
            $temp = array();
            $temp['crouse_id'] = $v->crouse_id;
            $temp['classroom_id'] = $v->classroom_id;
            $temp['teacher_id'] = $v->teacher_id;
            $temp['squads_id'] = $v->squads_id;
            $temp['period'] = $v->period;
            $temp['tutor_id'] = $v->tutor_id;
            $temp['section'] = $v->section;
            $temp['position'] = $v->position;
            $temp['start_time'] = $v->start_time;
            $temp['end_time'] = $v->end_time;
            $temp['memo'] = $v->memo;
            $temp['status'] = 1;
            $insert[] = $temp;
        }
        DB::transaction(function () use ($insert) {
            DB::table('crousesheets')->update(['status'=>-1]);
            DB::table('crousesheets')->insert($insert);
        });

        return array('status'=>1,'message'=>'操作成功');
    }
}
